/**
 * strings.js
 * Copyright (C) 2007-2011 Tommi Rautava
 * 
 * This file is part of Popmungo.
 *
 * Popmungo is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Popmungo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

org.mozdev.popmungo.strings = {
	_bundle: null,  // nsIStringBundle
	
	_stringBundleId: "popmungo.locale.stringbundle",
	
	LOCALE_CHROME_URL_BASE: "chrome://popmungo/locale/",
	
	BASIC_STRINGS_PROPS_FILE: "ui.properties",
	
	
	/**
	 * @param langId
	 * @param propsFile
	 */
	getBundleByLanguage: 
	function getBundleByLanguage(langId, propsFile)
	{
		try {
			/*
			 * Switch current locale temporately to 
			 * create the string bundle using the same language
			 * as user has selected at the game.
			 */
			var src = this.LOCALE_CHROME_URL_BASE + propsFile;
			org.mozdev.popmungo.logger.debug("landId="+ langId +", file="+ src);

			var uaPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("general.useragent.");
			var savedLocale = uaPrefs.getCharPref("locale");

			var locale = org.mozdev.popmungo.languageIdMap[langId];
			uaPrefs.setCharPref("locale", locale);

			var stringBundleService = Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService);

			stringBundleService.flushBundles();
			var bundle = stringBundleService.createBundle(src);
			
			// Test
			var iter = bundle.getSimpleEnumeration();
			if (iter.hasMoreElements()) {
				var prop = iter.getNext().QueryInterface(Components.interfaces.nsIPropertyElement);
				org.mozdev.popmungo.logger.debug("Test: "+ prop.key +"="+ prop.value);
			}

			uaPrefs.setCharPref("locale", savedLocale);

			org.mozdev.popmungo.logger.debug(locale +" stringbundle was loaded succesfully.");
			
			return bundle;
		}
		catch (err) {
			org.mozdev.popmungo.logger.logError("Error: Could not get "+ locale +" stringbundle: "+ err);
		}
			
		return null;
	},


	/**
	 * @param langId
	 */
	InitStringBundle: function InitStringBundle(langId) 
	{
		try {
			this._bundle = null;
			this._bundle = this.getBundleByLanguage(langId, this.BASIC_STRINGS_PROPS_FILE);

			if (this._bundle) {
				var testKey = "extensions.{9935ef86-61cd-4f48-a057-ab2c5172031d}.description";
				org.mozdev.popmungo.logger.debug("description="+ this._bundle.GetStringFromName(testKey));
				
				return true;
			}
		}
		catch (err) {
			org.mozdev.popmungo.logger.logError("Error: Could not get "+ org.mozdev.popmungo.languageIdMap[langId] +" stringbundle: "+ err);
		}

		// Default
		try {
			var stringBundleElement = document.getElementById(this._stringBundleId);
			this._bundle = stringBundleElement.stringBundle;
			
			org.mozdev.popmungo.logger.debug("The default stringbundle was loaded succesfully.");
			return true;
		}
		catch (err) {
			org.mozdev.popmungo.logger.logError("Error: Could not get the default stringbundle: "+ err);
		}
			
		return false;
	},
	
	
	/**
	 * @param aStringName
	 * @param aFormatArgs
	 */
	getString: function getString(aStringName, aFormatArgs) 
	{
		var s;

		try {
			if (aFormatArgs) {
				org.mozdev.popmungo.logger.debug(aStringName +" with format string: " + aFormatArgs.join("; "));
				
				s = this._bundle.formatStringFromName(aStringName, aFormatArgs, aFormatArgs.length);
			} else {
				s = this._bundle.GetStringFromName(aStringName);
			}
		}
		catch(err) {
			org.mozdev.popmungo.logger.logError(err);
		};

		org.mozdev.popmungo.logger.debug(aStringName +"='"+ s + "'");
		
		return s;
	},
	
	
	/**
	 * @param aStringName
	 * @deprecated Use {@link org.mozdev.popmungo.strings#getString}
	 */
	getLocalesString: function getLocalesString(aStringName) {
		var s;
		
		try {
			if (this._bundle.GetStringFromName) {
				s = this._bundle.GetStringFromName(aStringName);
			}
			else {
				s = this._bundle.getString(aStringName);
			}
		}
		catch(err) {
			org.mozdev.popmungo.logger.logError(err);
		};
		
		return s;
	}
};
